package com.lrm.web;

import com.lrm.biz.IBlogBiz;
import com.lrm.biz.ITagBiz;
import com.lrm.pojo.Blog;
import com.lrm.pojo.Tag;
import com.lrm.util.Result;
import com.lrm.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/12/7.
 * @Modified By:
 */
@RestController
@RequestMapping("tagshow")
public class TagShowController {

    @Autowired
    private ITagBiz tagBiz;

    @Autowired
    private IBlogBiz blogBiz;

    @GetMapping("listTag")
    public Result listTag() {
        List<Tag> tags = tagBiz.listTag(10000);
        return ResultUtil.success(tags);
    }

    @GetMapping("/pageblog/{tagId}")
    public Result pageblog(@PageableDefault(size = 10, sort = {"updataTime"}, direction = Sort.Direction.DESC)
                                   Pageable pageable, @PathVariable Long tagId) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Blog> blogs;
        if (tagId == -1) {
            List<Tag> tags = tagBiz.listTag(10000);
            tagId = tags.get(0).getId();
        }
        blogs = blogBiz.listBlog(pageable, tagId);
        //type的选中状态
        map.put("typeid", tagId);
        map.put("blogs", blogs);
        return ResultUtil.success(map);
    }
}
